//
//  ResponseHandlerExample.swift
//  YD_DataModel
//
//  Created by 钟志远 on 2021/11/29.
//  Copyright © 2021 alyadan. All rights reserved.
//

import Foundation
import SwiftyJSON
import RxSwift
import Moya

open class ResponseHandlerExample: ResponseHandler {
    
    
    public var codeName = "code"
    public var messageName = "message"
    public var dataName = "data"
    
    public var successCodeRange = 200..<300
    public var authFailCode = 401
    
    open func dealResponse<T: TargetType>(response: Response, target: T) throws -> Observable<JSON> {
        
        let json = JSON(response.data)
        let code = json[codeName].intValue
        let message = json[messageName].stringValue
        let contentData = json[dataName]
        
        if response.statusCode == authFailCode {
            return try dealAuthError(response: response, target: target)
        }
        
        guard successCodeRange.contains(response.statusCode) else {
            throw YDNetworkTool.HttpError.serverError(response.request, response.statusCode, message, json)
        }
//        guard let data = response.data else {
//            throw YDNetworkTool.HttpError.serverError(statusCode, "", JSON.init(data as Any))
//        }
        
//        let json = JSON(data)
//        let code = json[codeName].intValue
//        let message = json[messageName].stringValue
//        let contentData = json[dataName]
//
        guard successCodeRange.contains(code) else {
            guard code != authFailCode else {
                throw YDNetworkTool.HttpError.serverError(response.request, code, message, json)
            }
            throw YDNetworkTool.HttpError.serverError(response.request, code, message, json)
        }

        return Observable.just(contentData)
    }
    
    open func dealAuthError<T: TargetType>(response: Response, target: T) throws -> Observable<JSON> {
        let json = JSON(response.data)
        let code = json[codeName].intValue
        let message = json[messageName].stringValue
        throw YDNetworkTool.HttpError.serverError(response.request, code, message, json)
    }
}

open class ResponseHandlerExampleJustData: ResponseHandler {
    public var messageName = "message"
    
    public var successCodeRange = 200..<300
    public var authFailCode = 401
    
    open func dealResponse<T>(response: Response, target: T) throws -> Observable<JSON> where T : TargetType {
        let json = JSON(response.data)
        let message = json[messageName].stringValue
        
        if response.statusCode == authFailCode {
            return try dealAuthError(response: response, target: target)
        }
        
        guard successCodeRange.contains(response.statusCode) else {
            throw YDNetworkTool.HttpError.serverError(response.request, response.statusCode, message, json)
        }
        
        return Observable.just(json)
    }
    
    open func dealAuthError<T>(response: Response, target: T) throws -> Observable<JSON> where T : TargetType {
        let json = JSON(response.data)
        let message = json[messageName].stringValue
        throw YDNetworkTool.HttpError.serverError(response.request, authFailCode, message, json)
    }
    
    
    
    public init(){}
    
    
}
